package org.bao.bestmatch;

import java.util.LinkedList;

import org.bao.bestmatch.model.Matrix;
import org.bao.bestmatch.model.TFT_CFPair;
import org.bao.bestmatch.utils.HungarianAlgorithm;

/**
 * <b>HungarianImp</b> uses <b>HungarianAlgorithm</b> to compute the best-match route.
 * @author baoxiwu<bb@feijiao.info>,Cell&Module,BOE DT
 * @version 0.9.0
 * @since 2010-11-21
 * @license LGPL
 * 
 */
public class HungarianImp {
	public LinkedList<TFT_CFPair> compute(Matrix matrix){
		LinkedList<TFT_CFPair> pair = new LinkedList<TFT_CFPair>();
		
		HungarianAlgorithm ha = new HungarianAlgorithm();
		
		float[][] hunMatrix = new float[matrix.size()][matrix.size()];
		
		//TODO matrix[x][y] : x === y? 
		for(int i = 0; i < matrix.size(); i++){
			for(int j = 0; j < matrix.size(); j++){
				hunMatrix[i][j] = matrix.get(i, j).getNG();
			}
		}
		
		int[][] result = ha.computeAssignments(hunMatrix);
		
		for(int i = 0; i < result.length; i++){
			pair.add(matrix.get(result[i][0], i));
			//System.err.println("info@HungarianImp>:\n" + matrix.get(i,result[i][0]));
		}
		
		return pair;
	}
}
